# coding: UTF-8
import win32gui, win32con, win32api
import time
import datetime
import logging

timesleep = 5
title = ("wps")
curday = 0


def EnumWinCall(hwnd, nouse):
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd):
        wintitle = win32gui.GetWindowText(hwnd)
        if wintitle == ("\xc3\xdc\xc2\xeb"):    # str=u"密码" str.encode('gb2312')
            logging.info("find mima hwnd: %X, %s" % (hwnd, wintitle))
            win32api.SendMessage(hwnd, win32con.WM_CLOSE, 0, 0)
        wintitle = wintitle.lower()
        if wintitle.find(title) != -1:
            logging.info("find wind hwnd: %X, %s" % (hwnd, wintitle))
            win32api.SendMessage(hwnd, win32con.WM_CLOSE, 0, 0)


def mainLoop():
    nowdate = datetime.datetime.now()
    logname = ("killWinTitle%s-%s-%s.log" % (nowdate.year, nowdate.month, nowdate.day))
    logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s ',
                            datefmt='%Y-%m-%d %H:%M:%S', filename=logname, filemode='w')
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-5s : %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    logging.info("start monitor window title: %s" % (title))
    global curday
    while True:
        nowdate = datetime.datetime.now()
        if nowdate.day != curday:
            curday = nowdate.day
            logging.info("monitor loop %s-%s-%s" % (nowdate.year, nowdate.month, nowdate.day))
        win32gui.EnumWindows(EnumWinCall, 0)
        time.sleep(timesleep)


mainLoop()
